//
//  JlaiMuseumMap.m
//  JlaiMuseumMap
//
//  Created by Apple on 2018/1/16.
//  Copyright © 2018年 JLAI. All rights reserved.
//

#import "JlaiMuseumMap.h"
#import "FMMapKit.h"

@interface JlaiMuseumMap () <UIDocumentInteractionControllerDelegate>

@end

@interface JlaiMuseumMap () <FMKMapViewDelegate,FMKNaviAnalyserDelegate>
    //@public FMKMapView * _mapView
    @property (nonatomic,strong) FMKMapView * mapView;
    +(id)sharedMuseumMap;
@end



@implementation JlaiMuseumMap

FMKLocationMarker * locaitonMakerXY;
FMKNaviConstraint * _naviConstraint;
CGPoint _moveAnimationTemp;

+(id)sharedMuseumMap {
    
    static JlaiMuseumMap* sharedMuseumMap = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMuseumMap = [[self alloc] init];
    });
    return sharedMuseumMap;
}


/**
 *  获取FMKMapView * 可使用室内地图底层API
 *
 */
+(id)GetMapView{
    
    JlaiMuseumMap* _map = [JlaiMuseumMap sharedMuseumMap];
    return _map.mapView;
}


/**
 *  初始化视图
 *
 *  @param uiView 外部传入UIView地址
 */
+(void) InitialView:(UIView *) uiView
{
    
    NSLog(@"^^^^^^^^ InitialView ^^^^^^^^" );
    /*
    CGRect rect = CGRectMake(100, 200, 150, 50);
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    label.text = @"文本信息"; //设置内容
    label.textColor = [UIColor redColor];
    NSLog(@"%@", label.text); //读取内容
    [uiView addSubview: label];
    */
    
    //[[FMKMapSDK shareSDK] setKey:@"7c1888ed8de4300c8d71f9528e569657"];
    
    [[JlaiMuseumMap sharedMuseumMap] InitialMap:uiView] ;
    
}

/**
 *  初始化室内地图
 *
 *  @param uiView 外部传入UIView地址
 */
-(void) InitialMap:(UIView *) uiView
{
    
    NSLog(@"===InitialMap===");
    
    // 离线地图加载
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"jlai-cyds-museum02" ofType:@"fmap"];
    NSLog(@"%@", dataPath);
    
    _mapView = [[FMKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) path:dataPath delegate:self];
    // _mapView.enable3D = NO;///
    NSLog(@"%@", _mapView);
    
    [uiView addSubview:_mapView];
    
    
    NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"jlai-cyds-museum02/jlai-cyds-museum02.theme"];
    NSLog(@"%@", themePath);
    
    [_mapView setThemeWithLocalPath:themePath];
    
    // 通过地图数据路径初始化路径分析
    _naviConstraint = [[FMKNaviConstraint alloc] initWithMapPath:dataPath];
    
    locaitonMakerXY = [[FMKLocationMarker alloc]initWithPointerImageName:@"pointer0" DomeImageName:(@"dome.png")];
    //[locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943478.9942, 5437721.4263))];
    [_mapView.map.locationLayer addMarker:locaitonMakerXY];
    locaitonMakerXY.size = CGSizeMake(32, 32);
}



/**
 *  直接在地图上显示坐标点
 *
 *  @param point 用户所在位置的坐标  世界坐标(墨卡托)
 */
+(void) SetPosition:(CGPoint)point
{
    //NSLog(@"Hahah >>( %f,%f) ", point.x , point.y);
    [locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(point.x, point.y))];
}




/**
 * 在地图上显示转换后的坐标点
 *
 * @param point 用户所在位置的坐标  局部坐标
 */
+(void) SetPositionWithOffset:(CGPoint)point
{    
     //NSLog(@"Hahah >>( %f,%f) ", point.x , point.y);
     [locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943478.9942+point.x, 5437721.4263-point.y))];
}



/**
 * 在地图上显示转换后的坐标点
 *
 * @param lastPoint 用户所在上一个位置的坐标（可以与curPoint为同一个点）  局部坐标
 * @param curPoint  用户当前所在位置的坐标  局部坐标
 * @return 若约束计算成功则返回真，否则返回假
 */
+(bool) SetPositionWithConstraintByLast:(CGPoint)lastPoint currentPoint:(CGPoint)curPoint
{
    //NSLog(@"SetPositionWithConstraintByLast >>( %f,%f) ", curPoint.x , curPoint.y);
    FMKNaviConstraintResult cr = [_naviConstraint pathContraintByLastPoint:FMKMapPointMake(13943478.9942+lastPoint.x, 5437721.4263-lastPoint.y) currentMapPoint:(FMKMapPointMake(13943478.9942+curPoint.x, 5437721.4263-curPoint.y)) groupID:@"1"];
    
    //NSLog(@"%l",cr.type);//FMKCONSTRAINT_SUCCESS
    //NSLog(@"%f",cr.distance);
    //NSLog(@"---(%f,%f)",cr.mapPoint.x,cr.mapPoint.y);
    
    if(cr.type == FMKCONSTRAINT_SUCCESS){
        [locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(cr.mapPoint.x,cr.mapPoint.y))];
        return true;
    }else{
        [locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943478.9942+curPoint.x, 5437721.4263-curPoint.y))];
        return false;
    }
    
}



/**
 * 在地图上显示约束的点（可取得约束点坐标）
 *
 * @param lastPoint  用户所在上一个位置的坐标（可以与curPoint为同一个点） 局部坐标
 * @param curPoint  用户当前所在位置的坐标  局部坐标
 * @param point  返回约束计算后的点的墨卡托坐标 世界坐标
 * @return  若约束计算成功则返回真，否则返回假
 */
+(bool) SetPositionWithConstraintByLast:(CGPoint)lastPoint currentPoint:(CGPoint)curPoint getResultPoint:(CGPoint *)point
{
   // NSLog(@"SetPositionWithConstraintByLast >>( %f,%f) ", curPoint.x , curPoint.y);
    FMKNaviConstraintResult cr = [_naviConstraint pathContraintByLastPoint:FMKMapPointMake(13943478.9942+lastPoint.x, 5437721.4263-lastPoint.y) currentMapPoint:(FMKMapPointMake(13943478.9942+curPoint.x, 5437721.4263-curPoint.y)) groupID:@"1"];
    
    //NSLog(@"%l",cr.type);//FMKCONSTRAINT_SUCCESS
    //NSLog(@"%f",cr.distance);
    //NSLog(@"---(%f,%f)",cr.mapPoint.x,cr.mapPoint.y);
    
    if(cr.type == FMKCONSTRAINT_SUCCESS){
        [locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(cr.mapPoint.x,cr.mapPoint.y))];
        if(point){
            point->x = cr.mapPoint.x;
            point->y = cr.mapPoint.y;
        }
        return true;
    }else{
        [locaitonMakerXY locateWithGeoCoord:FMKGeoCoordMake(1, FMKMapPointMake(13943478.9942+curPoint.x, 5437721.4263-curPoint.y))];
        return false;
    }
}





/**
 * 仅获取当前点约束后的墨卡托坐标 世界坐标
 *
 * @param lastPoint  用户所在上一个位置的坐标（可以与curPoint为同一个点） 局部坐标
 * @param curPoint  用户当前所在位置的坐标  局部坐标
 * @param point  返回约束计算后的点的墨卡托坐标 世界坐标
 * @return  若约束计算成功则返回真，否则返回假
 */
+(bool) GetPointWithConstraintByLast:(CGPoint)lastPoint currentPoint:(CGPoint)curPoint getResultPoint:(CGPoint *)point
{
    //NSLog(@"SetPositionWithConstraintByLast >>( %f,%f) ", curPoint.x , curPoint.y);
    FMKNaviConstraintResult cr = [_naviConstraint pathContraintByLastPoint:FMKMapPointMake(13943478.9942+lastPoint.x, 5437721.4263-lastPoint.y) currentMapPoint:(FMKMapPointMake(13943478.9942+curPoint.x, 5437721.4263-curPoint.y)) groupID:@"1"];
    
    //NSLog(@"%l",cr.type);//FMKCONSTRAINT_SUCCESS
    //NSLog(@"%f",cr.distance);
    //NSLog(@"---(%f,%f)",cr.mapPoint.x,cr.mapPoint.y);
    
    if(cr.type == FMKCONSTRAINT_SUCCESS){
        if(point){
            point->x = cr.mapPoint.x;
            point->y = cr.mapPoint.y;
            return true;
        }
    }
    
    if(point){
        point->x = curPoint.x;
        point->y = curPoint.y;
    }
    return false;
    
}



/**
 * 设置等角视图模式
 * 
 */
+(void) SetIsometricView
{
    JlaiMuseumMap* _map = JlaiMuseumMap.sharedMuseumMap;
    
    // 设置地图显示级别
    [_map.mapView.map setZoomLevel:21 animated:NO];
    _map.mapView.map.mapStatus.zoomScale =1.5;
    
    // 设置地图中心点
    FMKGeoCoord _mapCenter = FMKGeoCoordMake(1, FMKMapPointMake((_map.mapView.map.info.minX+_map.mapView.map.info.maxX)/2-3, (_map.mapView.map.info.minY+_map.mapView.map.info.maxY)/2-5));
    
    //_mapCenter = FMKGeoCoordMake(1, FMKMapPointMake(120, 500));
    [_map.mapView moveToViewCenterFromMapPoint:_mapCenter.mapPoint animated:YES];
    // 设置地图旋转角度
    _map.mapView.map.mapStatus.rotateAngle = 0;
    // 设置地图倾斜角度
    _map.mapView.map.mapStatus.inclineAngle = 45;

}





#pragma mark - FMKMapViewDelegate
- (void)mapViewDidFinishLoadingMap:(FMKMapView *)mapView {
    NSLog(@"地图加载完成");
}
- (void)mapViewDidFailLoadingMap:(FMKMapView *)mapView withError:(NSError *)error {
    NSLog(@"地图加载失败-%@", error);
}

@end
